package com.unione.map.view.fragment

import android.view.LayoutInflater
import android.view.View
import android.view.View.GONE
import android.view.View.OnClickListener
import android.view.View.VISIBLE
import android.view.ViewGroup
import androidx.fragment.app.viewModels
import androidx.lifecycle.lifecycleScope
import androidx.navigation.Navigation
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.core.BasePopupView
import com.lxj.xpopup.impl.LoadingPopupView
import com.unione.map.R
import com.unione.map.constant.AppConstant
import com.unione.map.databinding.FragmentEditMapBgBinding
import com.unione.map.databinding.FragmentEditMapStep1Binding
import com.unione.map.databinding.FragmentEditMapStep2Binding
import com.unione.map.view.act.MainActivity
import com.unione.map.view.viewmodel.CreateMapStep1ViewModel
import com.unione.map.view.widget.EnumMapMode
import com.unione.unione_protocol.UnioneRobotRepository
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

class EditMapBgFragment : AppBaseFragment<FragmentEditMapBgBinding>(), OnClickListener {

    private val viewModel: CreateMapStep1ViewModel by viewModels()
    private lateinit var loadingPopupView: LoadingPopupView
    private var mLoadingPopup: BasePopupView? = null
    private var mCurrentSquareSize = AppConstant.originSquareSize

    companion object {
        const val TAG = "EditMapStep2Fragment"

    }

    override fun onCreateViewBinding(
        inflater: LayoutInflater,
        parent: ViewGroup?
    ): FragmentEditMapBgBinding {
        return FragmentEditMapBgBinding.inflate(inflater, parent, false)
    }

    override fun init() {
        initView()
        initListeners()
    }


    private fun initView() {

    }


    private fun initListeners() {
        binding.ivAdd.setOnClickListener(this)
        binding.ivSub.setOnClickListener(this)
        binding.btnReset.setOnClickListener(this)
        binding.clRefresh.setOnClickListener(this)
        binding.clClean.setOnClickListener(this)
        binding.btnBack.setOnClickListener(this)
        binding.btnFinish.setOnClickListener(this)
    }

    override fun onClick(v: View?) {
        super.onClick(v)
        if (v == null) return
        val nvController =
            Navigation.findNavController(
                requireActivity() as MainActivity,
                R.id.fragment_container_view
            )
        when (v.id) {
            R.id.ivAdd -> {
                mCurrentSquareSize++
                binding.tvRange.text = mCurrentSquareSize.toString()
                binding.mapView.setSquareSize(mCurrentSquareSize.toFloat())
            }

            R.id.ivSub -> {
                if (mCurrentSquareSize > AppConstant.originSquareSize) {
                    mCurrentSquareSize--
                    binding.tvRange.text = mCurrentSquareSize.toString()
                    binding.mapView.setSquareSize(mCurrentSquareSize.toFloat())
                }
            }

            R.id.btnReset -> {
                mCurrentSquareSize = AppConstant.originSquareSize
                binding.tvRange.text = mCurrentSquareSize.toString()
                binding.mapView.setSquareSize(mCurrentSquareSize.toFloat())
            }

            R.id.clRefresh -> {
                // 重新获取地图
                lifecycleScope.launch{
                    UnioneRobotRepository.getMapList()
                }
            }

            R.id.clClean -> {
                binding.mapView.clearAllSquares()
            }

            R.id.btnFinish -> {
                if (binding.btnFinish.text.toString() == "开始编辑") {
                    binding.mapView.setMapMode(EnumMapMode.MODE_EDIT)
                    binding.btnFinish.text = "结束编辑"
                } else {
                    binding.mapView.setMapMode(EnumMapMode.MODE_NORMAL)
                    binding.btnFinish.text = "开始编辑"
                }
            }

            R.id.btnNext -> {
                nvController.navigate(R.id.createMapStep2Fragment)
            }
        }
    }
}